﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using DevExpress.Web.ASPxCallback;
using DevExpress.XtraCharts.Web;

namespace TireService.Web.UserControls.StatusOverview
{
    public partial class ucTaskOverview : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            if (Page.IsPostBack != true)
            {
                // Bind dropdownlist TaskType
                ddlTaskType.DataSource = db.GetTaskTypes();
                ddlTaskType.ValueField = "TaskTypeId";
                ddlTaskType.TextField = "Description";
                ddlTaskType.DataBind();
                ddlTaskType.SelectedIndex = 1;

                BindTaskTypeData(db, Convert.ToInt32(ddlTaskType.SelectedItem.Value));
            }
        }

        /// <summary>
        /// Bind chart data
        /// </summary>
        /// <param name="db">Connection to database</param>
        /// <param name="TaskTypeId">Id on tasktype to show</param>
        private void BindTaskTypeData(TireServiceDB db, int TaskTypeId)
        {
            var tasktypecount = from p in db.Tasks
                                join y in db.TaskTypes on p.TaskTypeId equals y.TaskTypeId into py
                                from z in py.DefaultIfEmpty()
                                join v in db.TireSets on p.TireSetId equals v.TireSetId into zv
                                from x in zv.DefaultIfEmpty()
                                join b in db.ContainerCargoRooms on x.ContainerCargoRoomId equals b.ContainerCargoRoomId into xb
                                from n in xb.DefaultIfEmpty()
                                join a in db.TaskTypeStatus on p.TaskTypeStatusId equals a.TaskTypeStatusId into pa
                                from c in pa.DefaultIfEmpty()
                                join o in db.Containers on n.ContainerId equals o.ContainerId into no
                                from s in no.DefaultIfEmpty()
                                where z.TaskTypeId == TaskTypeId
                                group n by new
                                {
                                    s.ContainerNumber,
                                    TaskTypeStatusDescription = c.Description
                                }
                                    into grp
                                    select new
                                    {
                                        grp.Key.ContainerNumber,
                                        grp.Key.TaskTypeStatusDescription,
                                        TaskTypeStatusCount = grp.Count()
                                    };

            chartTaskType.SeriesDataMember = "ContainerNumber";
            chartTaskType.SeriesTemplate.ArgumentDataMember = "TaskTypeStatusDescription";
            chartTaskType.SeriesTemplate.ArgumentScaleType = DevExpress.XtraCharts.ScaleType.Qualitative;
            chartTaskType.SeriesTemplate.ValueScaleType = DevExpress.XtraCharts.ScaleType.Numerical;
            chartTaskType.SeriesTemplate.ValueDataMembers[0] = "TaskTypeStatusCount";

            chartTaskType.DataSource = tasktypecount;
            chartTaskType.DataBind();
        }

        /// <summary>
        /// Callback function that refreshes Chart
        /// </summary>
        protected void chartTaskType_CustomCallback(object sender, CustomCallbackEventArgs e)
        {
            TireServiceDB db = new TireServiceDB();

            BindTaskTypeData(db, Convert.ToInt32(e.Parameter));
        }

    }
}